import * as React from "react";
import Modal from "antd/es/modal/Modal";
import {Button, Card, Col, Form, Input, InputNumber, notification, Row} from "antd";
import {PlusOutlined, UserOutlined} from "@ant-design/icons";
import {createKey, updateKey, updateOwnKey} from "../../requests/KeyManageRequest";


class UpdateVerifyKeyModal extends React.Component{

    constructor(props) {
        super(props);
        this.state = {
            isModalVisible: false,
            formRef: React.createRef(),
            isOwn:this.props.isOwn
        };
        this.props.onRef(this)
    }

    showModal = () => {
        this.setState({
            isModalVisible: true
        })
    };

    handleCancel = () => {
        this.setState({
            isModalVisible: false
        })
    };

    handleOk = () => {

        if (!this.props.keyName){
            notification.error({message: "更新激活码失败", description: "请在表格中选择激活码之后进行更新！"});
            return
        }

        this.state.formRef.current.validateFields()
            .then(values => {
                values['keyName'] = this.props.keyName
                if (this.state.isOwn===true){
                    console.log("Begin to update own verify key with params:" + JSON.stringify(values))
                    updateOwnKey(values).then(edpKey => {
                        notification.info({message: "更新激活码成功", description: "更新激活码" + edpKey.result.keyName + "成功"});
                    })
                }else {
                    console.log("Begin to update all key with params:" + JSON.stringify(values))
                    updateKey(values).then(edpKey => {
                        notification.info({message: "更新激活码成功", description: "更新激活码" + edpKey.result.keyName + "成功"});
                    })
                }

                this.setState({
                    isModalVisible: false
                })
            })
            .catch(info => {
                console.log('Validate message:', info);
            });
    };

    render = () => {
        return (
            <Modal title={"更新激活码"+this.props.keyName} visible={this.state.isModalVisible} onOk={this.handleOk} onCancel={this.handleCancel}>
                <Form name="basic" ref={this.state.formRef}>
                    <Form.Item
                        label="增加激活码可用总次数(空表示不变)" name="incTimes">
                        <InputNumber prefix={<PlusOutlined/>}  />
                    </Form.Item>

                    <Form.Item
                        label="增加激活码可用天数(空表示不变)" name="incEffectiveDay">
                        <InputNumber/>
                    </Form.Item>

                    <Form.Item
                        label="修改激活码的发放对象(空表示不变)" name="username">
                        <Input/>
                    </Form.Item>

                    <Form.Item
                        label="修改激活码的备注信息(空表示不变)" name="remark">
                        <Input/>
                    </Form.Item>
                </Form>
            </Modal>
        );
    }
}

export default UpdateVerifyKeyModal